<?php
require_once '../inc/configuration.php';
require_once '../inc/connexion_bdd.php';
require_once '../inc/fonctions_sql.php';

$message_action = "";

$action = isset($_POST["action"]) ? $action = $_POST["action"] : "";

if(isset($_GET["id"])) {
    $id = $_GET["id"];
}
else if(isset($_POST["id"])) {
    $id = $_POST["id"];
}
else {
    $id = "";
}

if($action==1) {
    
    $tab_name_form = array(
        "id_entreprise",
        "nom",
        "prenom",
        "telephone",
//        "photo",
        "email",
        "observations"
    );
    
    $message_action = modifier_donnees("contacts", $tab_name_form);
}

$sql = "SELECT * FROM contacts WHERE id=".$id;
$envoi_requete = mysqli_query($connexion_serveur, $sql);
$data = mysqli_fetch_array($envoi_requete);
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Modifier contacts</title>
         <?php require_once "../inc/css_bootstrap.php"; ?>
        <style>
            .table {
                width: 500px;
            }
            
            th {
                width: 35%;
                text-align: right;
            }
            textarea.form-control {
                /*background-color: #ccc;*/
                height: 150px;
            }
            
        </style>
    </head>
    <body>
        <?php require_once '../inc/menu_principal.php'; ?>
        
        <h1>Modifier un contact</h1>
        <?php if(!empty($message_action)) { ?>
            <div id="message_action" class="alert <?php echo $message_action["css_class"]; ?>"><?php echo $message_action["texte"]; ?></div>
        <?php } ?>
            
        <form name="form_ajouter" id="form_ajouter" method="post" enctype="multipart/form-data">
            <table class="table table-bordered">
                    <tr>
                        
                        <th>
                            Entreprise : 
                        </th>
                        <td>
                            <?php 
                        //REQUETE SQL
                        $sql_ent = "SELECT id, raison_sociale FROM entreprises";
                        $req_ent = mysqli_query($connexion_serveur, $sql_ent);                        
                        ?>
                        <select name="id_entreprise" id="id_entreprise" class="form-control">
                            <?php
                            while($data_ent = mysqli_fetch_assoc($req_ent)) {
                            
                            if($data_ent["id"]==$data["id_entreprise"]) { $selected = "selected"; } else { $selected = ""; }
                            ?>
                            <option value="<?php echo $data_ent["id"]; ?>" <?php echo $selected; ?>><?php echo $data_ent["raison_sociale"]; ?></option>
                            <?php
                            }
                            ?>
                        </select>
                        </td>
                        
                    </tr>

                    <tr>
                        <th>Nom, Prenom</th>
                    <td>
                        <input type="text" name="nom" id="nom" class="form-control" required placeholder="NOM..." value="<?php echo $data["nom"]; ?>"> 
                        <input type="text" name="prenom" id="prenom" class="form-control" required placeholder="Prenom..." value="<?php echo $data["prenom"]; ?>">
                    </td>
                    </tr>
                    <tr>
                        <th>
                            Telephone : 
                        </th>
                        <td>
                           <input type="tel" name="telephone" id="telephone" class="form-control" value="<?php echo $data["telephone"]; ?>">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            Email : 
                        </th>
                        <td>
                           <input type="email" name="email" id="email" class="form-control" value="<?php echo $data["email"]; ?>">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            Photo : 
                        </th>
                        <td>
                            <input type="file" name="photo" id="photo" class="form-control">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            Observations : 
                        </th>
                        <td>
                            <textarea name="observations" id="observations" class="form-control"><?php echo $data["observations"]; ?></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <button type="submit" name="action" class="btn btn-warning" value="1"><span class="glyphicon glyphicon-ok"></span> Modifier</button>
                        </td>
                    </tr>
                </table>
        </form>
    </body>
</html>
